// program/pages/rentCar/rentCar.js
const app = getApp();
const service = require('../../utils/service.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShow: false,
        themeColor: '#ffd00a',
        calendarType: 'yydates',

        beginTime: '',
        beginTimeDay: '',
        endTime: '',
        endTimeDay: '',
        dayCount: '',

        branchList: [],
        // 当前打开的网点
        currentBranch: 0,
        // 价格弹窗显示状态
        priceCalendarDialogVisible: false,
        // 价格数据
        priceCalendar: []
    },

    bindSelectTime() {
        this.setData({
            isShow: true
        })
    },

    _yybindchange(e) {
        var data = e.detail;
        console.log(e)
        this.setData({
            beginDate: app.utils.parseTime(new Date(data.startTime), '{y}-{m}-{d}'),
            beginTime: app.utils.parseTime(new Date(data.startTime), '{m}月{d}日'),
            beginTimeDay: app.utils.parseTime(new Date(data.startTime), '周{a}'),
            endDate: app.utils.parseTime(new Date(data.endTime), '{y}-{m}-{d}'),
            endTime: app.utils.parseTime(new Date(data.endTime), '{m}月{d}日'),
            endTimeDay: app.utils.parseTime(new Date(data.endTime), '周{a}'),
            dayCount: data.diff+1
        })
    },

    bindBranchClick(e) {
        this.setData({
            currentBranch: e.currentTarget.dataset.idx
        })
    },

    toBranchInfo(e) {
        wx.navigateTo({
            url: '/pages/rentCar/branch/info?id=' + e.currentTarget.dataset.id,
        })
    },

    // 显示价格日历
    showPriceCalendar(e) {
        this.setData({
            priceCalendarDialogVisible: true,
            currentCarId: e.currentTarget.dataset.id
        })
    },
    getCalendarData(e) {
        console.log('------------')
        console.log(e)
    },
    // 隐藏价格日历
    hidePriceCalendar() {
        this.setData({
            priceCalendarDialogVisible: false
        })
    },

    // 监听日历数据
    getCalendarData(e) {
        console.log(e.detail)
    },

    // 获取网点列表
    getRentCarBranchList() {
        let that = this;
        let params = {
            latitude: this.data.addressFromLatitude,
            longitude: this.data.addressFromLongitude
        };
        service.getConnect(service.api.getRentCarBranchList, params).then(res => {
            that.setData({
                branchList: res.data
            })
            wx.hideLoading();
        })
    },

    // 下单
    toOrder(e){
        let params = {
            takeOutletId: e.currentTarget.dataset.id,
            ocId: e.currentTarget.dataset.item.id,
            alsoOutletId: e.currentTarget.dataset.id,
            startTime: this.data.beginDate,
            endTime: this.data.endDate,
        }
        wx.navigateTo({
          url: '/pages/rentCar/order/index?params='+JSON.stringify(params)
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let rentCar = JSON.parse(options.rentCar);
        this.setData(rentCar);
        this.getRentCarBranchList();
        wx.setNavigationBarTitle({
            title: '选择车型'
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        app.pageOnloadInit(this, app);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})